<template>
  <div class="divBox">
    <el-card class="box-card" shadow="hover">
      <template class="clearfix" v-slot:header>
        <div class="container">
          <el-form :model="tableFrom" label-width="100px" ref="replyRefs" class="replyCls">
            <el-row :gutter="20">
              <el-col :span="24">
                <el-form-item label="评论类型：">
                  <el-radio-group v-model="tableFrom.replyType" @change="tabSelectChange" class="mr20" size="small">
                    <el-radio-button :label="item.val" v-for="(item, index) in replyRadioList" :key="index">{{ item.label }}</el-radio-button>
                  </el-radio-group>
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item label="删除状态：">
                  <el-radio-group v-model="tableFrom.isDel" @change="tabSelectChange" class="mr20" size="small">
                    <el-radio-button :label="99">全部</el-radio-button>
                    <el-radio-button :label="0">未删除</el-radio-button>
                    <el-radio-button :label="1">已删除</el-radio-button>
                  </el-radio-group>
                </el-form-item>
              </el-col>
              <el-col v-bind="grid">
                <el-form-item label="评论状态：" class="selectFormWidth100">
                  <el-select v-model="tableFrom.isReply" placeholder="请选择评价状态" size="small">
                    <el-option label="全部" :value="99"></el-option>
                    <el-option label="已回复" :value="0"></el-option>
                    <el-option label="待回复" :value="1"></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col v-bind="grid">
                <el-form-item label="商品信息：">
                  <el-input v-model="tableFrom.productInfo" placeholder="请输入商品ID或者商品信息" size="small" clearable></el-input>
                </el-form-item>
              </el-col>
              <el-col v-bind="grid">
                <el-form-item label="用户名称：">
                  <el-input v-model="tableFrom.nickName" placeholder="请输入" size="small" clearable></el-input>
                </el-form-item>
              </el-col>
              <el-col :xs="24" :sm="12" :md="12" :lg="3" :xl="3">
                <el-form-item label-width="0">
                  <el-button icon="el-icon-search" size="small" type="primary" @click="getReplyList('1')">搜索</el-button>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
        </div>
        <el-button size="small" type="primary" icon="el-icon-plus" @click="replyAdd">添加虚拟评论</el-button>
        <el-tooltip :disabled="!disabledReply" class="ml10" effect="dark" content="本页至少选择一项" placement="bottom">
          <div style="display: inline-block;">
            <el-button :disabled="disabledReply" size="small" icon="el-icon-close" type="primary" @click="batchDelReply(null)">批量删除评论</el-button>
          </div>
        </el-tooltip>
      </template>
      <el-table
        v-loading="replyLoading"
        :data="replyList"
        style="width: 100%;"
        size="mini"
        class="table"
        @selection-change="handleSelectionReplyChange"
      >
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column prop="id" label="ID" min-width="50"></el-table-column>
        <el-table-column label="商品信息" min-width="400">
          <template slot-scope="{ row }">
            <div v-if="row.product && !productId" class="demo-image__preview acea-row row-middle">
              <template v-if="row.product">
                <div style="display: flex;" class="pictrue mr10" v-viewer>
                  <img v-lazy="$uploadURL(row.product.image)" :alt="row.product.title" style="width: 30px;height: 30px;">
                </div>
                <div class="info">{{ row.product.title }}</div>
              </template>
            </div>
            <div v-else class="demo-image__preview acea-row row-middle">
              <template v-if="appointProduct">
                <div style="display: flex;" class="pictrue mr10" v-viewer>
                  <img v-lazy="$uploadURL(appointProduct.image)" :alt="appointProduct.image" style="width: 30px;height: 30px;">
                </div>
                <div class="info">{{ appointProduct.title }}</div>
              </template>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="nickName" label="用户名称" min-width="100"></el-table-column>
        <el-table-column prop="productScore" label="商品评分" min-width="90"></el-table-column>
        <el-table-column prop="serviceScore" label="服务评分" min-width="90"></el-table-column>
        <el-table-column
          label="评价内容"
          min-width="210"
        >
          <template slot-scope="{ row }">
            <div class="mb5 content_font">{{row.comment}}</div>
            <template v-if="$util.splitArray(row.images, ',').length && $util.splitArray(row.images, ',')[0]">
              <div class="demo-image__preview" v-viewer>
                <img v-lazy="$uploadURL(item)" :alt="$uploadURL(item)" v-for="(item, index) in $util.splitArray(row.images, ',')" :key="index" class="mr5" style="width: 30px;height: 30px;">
              </div>
            </template>
            <div v-if="row.isDel == 1" style="color: rgb(237, 64, 20); display: block;">评论已删除</div>
          </template>
        </el-table-column>
        <el-table-column
          prop="replyContent"
          label="回复内容"
          min-width="250"
        />
        <el-table-column
          label="评价时间"
          min-width="120"
        >
          <template slot-scope="{ row }">
            <span v-if="row.replyDateUTC"> {{ row.replyDateUTC | uctTimeFileter }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" min-width="120" fixed="right" align="center">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="tabReply(scope.row.id)">回复</el-button>
            <el-divider direction="vertical"></el-divider>
            <el-button type="text" size="small" @click="handleDelete(scope.row.id, scope.$index)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="replyBox">
        <pagination
          v-show="pagination.total > 0"
          :total="pagination.total"
          :page.sync="pagination.page"
          :limit.sync="pagination.limit"
          :page-sizes="[10, 20, 30, 50]"
          @pagination="replyChange"
        />
      </div>
    </el-card>
  </div>
</template>

<script>
  import {
    getReplyListAllApi,
    delReplyDeleteApi,
    putReplyContentApi,
    batchDELReplyApi
  } from "@/api/store";
  import {
    productInfoApi as getProductInfoApi
  } from "@/api/product";

  export default {
    name: "index",
    data() {
      return {
        grid: {
          xl: 5,
          lg: 7,
          md: 12,
          sm: 12,
          xs: 24
        },
        replyRadioList: this.$constants.replyRadioType,
        tableFrom: {
          replyType: -99,
          isReply: 99,
          productInfo: '',
          nickName: '',
          isDel: 99
        },
        productId: this.$route.params.id === undefined ? '' : this.$route.params.id,
        appointProduct: {
          image: '',
          title: ''
        },
        replyLoading: false,
        disabledReply: true,
        replyList: [],
        // 选中的表格
        multipleSelectionReply: [],
        pagination: {
          page: 1,
          limit: 10,
          total: 0
        }
      }
    },
    watch: {
      '$route.params.id'(to, from) {
        this.productId = '';
        this.getReplyList('1');
      }
    },
    mounted() {
      this.getReplyList();
    },
    methods: {
      // 批量删除
      batchDelReply() {
        if (this.multipleSelectionReply.length <= 0) return this.$message.info("请至少选择一项");
        let arr = [];
        this.multipleSelectionReply.forEach((item) => arr.push(item.id));
        batchDELReplyApi(arr.join(',')).then(res => {
          this.$message.success('批量删除成功');
          this.$set(this.pagination, 'page', 1);
          this.getReplyList();
        });
      },
      handleSelectionReplyChange(val) {
        this.$set(this, 'multipleSelectionReply', val);
        this.multipleSelectionReply.length > 0 ? this.disabledReply = false : this.disabledReply = true
      },
      // 添加虚拟评论
      replyAdd() {
        const _this = this;
        this.$modal({
          componentProps: {},
          component: (resolve) => require(['./components/createReply'], resolve),
          props: {
            title: '添加虚拟评论',
            customClass: 'zdyDialogCls replyDialogCls',
            width: '100%'
          },
          methods: {
            getReplyList() {
              _this.$set(_this.pagination, 'page', 1);
              _this.getReplyList();
            }
          }
        });
      },
      // 删除
      handleDelete(id, idx) {
        this.$modalSure().then(res => {
          delReplyDeleteApi(id).then(res => {
            this.$message.success('删除成功')
            this.replyList.splice(idx, 1)
          })
        });
      },
      tabReply(id) {
        this.$prompt('', '回复内容', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          inputErrorMessage: '请输入回复内容',
          inputType: 'textarea',
          inputPlaceholder: '请输入回复内容',
          inputValidator: (value) => {
            if (!value) {
              return '输入不能为空';
            }
          }
        }).then(({ value }) => {
          putReplyContentApi(id, {
            ReplyContent: value
          }).then(res => {
            this.$message({
              type: 'success',
              message: '回复成功'
            });
            this.getReplyList();
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '取消输入'
          })
        })
      },
      /**
       * 获取产品列表
       */
      getReplyList(num) {
        this.$set(this, 'replyLoading', true);
        getReplyListAllApi({
          pageNumber: num ? num : this.pagination.page,
          pageSize: this.pagination.limit,
          orderBy: 'id desc',
          replyType: this.tableFrom.replyType == -99 ? 0 : this.tableFrom.replyType,
          isReply: this.tableFrom.isReply == 99 ? -1 : this.tableFrom.isReply,
          productInfo: this.tableFrom.productInfo,
          nickName: this.tableFrom.nickName,
          isDel: this.tableFrom.isDel == 99 ? -1 : this.tableFrom.isDel,
          productId: this.productId
        }).then(res => {
          this.$set(this, 'replyList', res.data);
          this.$set(this.pagination, 'total', res.head.totalCount);
          this.$set(this, 'replyLoading', false);
          if (this.productId && !res.data[0].product) {
            getProductInfoApi(this.productId).then(res => {
              this.$set(this, 'appointProduct', {
                image: res.image,
                title: res.title
              });
            });
          };
        }).catch(() => {
          this.$set(this, 'replyLoading', false);
          this.$set(this.pagination, 'total', 0);
          this.$set(this, 'replyList', []);
        });
      },
      replyChange(reply) {
        this.$set(this.pagination, 'page', reply.page);
        this.$set(this.pagination, 'limit', reply.limit);
        this.getReplyList();
      },
      tabSelectChange() {
        this.$set(this.pagination, 'page', 1);
        this.getReplyList();
      }
    }
  }
</script>

<style lang="scss" scoped>
  .demo-image__preview {
    img {
      cursor: pointer;
    }
    .info {
      width: 60%;
    }
  }
  .content_font {
    color: #2b85e4;
  }
</style>

<style lang="scss">
  .el-dialog.replyDialogCls {
    max-width: 700px;
  }
</style>
